//
//  main.m
//  homework-05
//
//  Created by Snow on 16/7/11.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "City.h"
#import "Zone.h"
#import "Province.h"
#import "TempCity.h"

int main(int argc, const char * argv[]) {
    //四、
    
    //处理省市区文件中的area.txt
    //将省市区进行分割处理, 省里包含市, 市里包含区
    //1. 先用数组字典表示
    //2. 用数组模型
    
    //1.省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
    NSString *path = @"/Users/Snow/DLS160710/Objective-C/ClassCode/05-遍历.排序/homework-05/homework-05/area.txt";
    //拿到数据源字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"%@", sourceString);
    //根据回车分割字符串
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    //总体的大数组
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    //NSLog(@"%@", allArray);
    for (NSString *area in allArray) {
        //取到所有的省, 判断前缀是否有空格
        if (![area hasPrefix:@" "]) {
            //创建了省字典(字典包含两个字段 name  array)
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            //添加ProName字段
            [proDic setObject:area forKey:@"proName"];
            //创建了所有市的数组
            NSMutableArray *cityArray = [NSMutableArray array];
            //添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            //将创建好的省字典 放到省数组中
            [allProvinceArray addObject:proDic];
            //取到所有的市(两个空格开头, 但不是4个空格开头)
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            //难点 通过lastObject获取到对应的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            //拿到字典中对应得市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            // 创建市字典(name 和区array)
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            [cityArray addObject:cityDic];
            //取到所有的区
        } else {
            //通过省数组获取到当前所在的省的字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            //通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            //通过市数组获取到对应的市字典
            NSMutableDictionary *cityDic = [cityArray lastObject];
            //通过市字典获取到对应得区或县的数组
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
            [zoneArray addObject:area];
        }
    }
//    NSLog(@"%@", allProvinceArray);
    //2.数组模型- province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@""]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:@"area"];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    //2
    NSString *path1 = @"/Users/Snow/DLS160710/Objective-C/ClassCode/05-遍历.排序/homework-05/homework-05/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    for (NSString *city in allCityArray) {
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        [allCityDicArray addObject:cityDic];
    }
    //排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
        } else if([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
//    NSLog(@"%@", finalArray);
    //2.2
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        //根据|切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    //对类数组 排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityName" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    
//    NSLog(@"%@", allTempCityArray);
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *temp in allTempCityArray) {
        if ([temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:temp];
            [finalClassArray addObject:pro];
        } else if ([temp.cityNumber hasSuffix:@"00"] && ![temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:temp];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:temp];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", finalClassArray);
    
    
    
    
    
    return 0;
}
